
/**
 * Copyright 2014-2020  the original author or authors.
 * <p>
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
 * in compliance with the License. You may obtain a copy of the License at
 * <p>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p>
 * Unless required by applicable law or agreed to in writing, software distributed under the License
 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
 * or implied. See the License for the specific language governing permissions and limitations under
 * the License.
 */
package com.upb.webserver.org;

import com.upb.webserver.account.AccountService;
import com.upb.webserver.common.pojo.account.AccountCommon;
import com.upb.webserver.common.pojo.base.BasePageResponse;
import com.upb.webserver.common.pojo.base.BaseResponse;
import com.upb.webserver.common.pojo.org.ReqAddOrgVO;
import com.upb.webserver.common.pojo.org.ReqUpdateOrgVO;
import com.upb.webserver.common.pojo.org.RspOrgVO;
import com.upb.webserver.common.returncode.ConstantCode;
import com.upb.webserver.common.tools.JsonUtils;
import com.upb.webserver.dao.entity.TbOrg;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

@Log4j2
@RestController
@RequestMapping(value = "org")
public class OrgController {

    @Autowired
    private AccountService accountService;
    @Autowired
    private OrgManager orgManager;
    @Autowired
    private OrgService orgService;


    /**
     * add new org.
     *
     * @param param
     * @param result
     * @return
     */
    @PostMapping
    public BaseResponse addOrg(@RequestBody @Valid ReqAddOrgVO param, BindingResult result, HttpServletRequest request) {
        log.info("start exec method [addOrg]. param:{}", JsonUtils.objToString(param));
        AccountCommon accountCommon = accountService.getAccountCommon(request);
        TbOrg orgResult = orgService.addOrg(accountCommon.getPlatform(), accountCommon.getId(), param);
        BaseResponse baseResponse = new BaseResponse(ConstantCode.SUCCESS);
        baseResponse.setData(orgResult);
        log.info("success exec method [addOrg]. baseResponse:{}", JsonUtils.objToString(baseResponse));
        return baseResponse;
    }


    /**
     * update org by id.
     *
     * @param param
     * @param result
     * @return
     */
    @PatchMapping
    public BaseResponse updateOrg(@RequestBody @Valid ReqUpdateOrgVO param, BindingResult result) {
        log.info("start exec method [updateOrg]. param:{}", JsonUtils.objToString(param));
        orgService.updateOrg(param);
        log.info("success exec method [updateOrg].");
        return new BaseResponse(ConstantCode.SUCCESS);
    }

    /**
     * query by page.
     *
     * @param pageNumber
     * @param pageSize
     * @return
     */
    @GetMapping(value = "/list")
    public BasePageResponse queryOrgByPage(@RequestParam(value = "pageNumber") Integer pageNumber,
                                           @RequestParam(value = "pageSize") Integer pageSize,
                                           @RequestParam(value = "developerId", required = false) Integer developerId,
                                           HttpServletRequest request) {
        log.info("start exec method [queryOrgByPage]. pageNumber:{} pageSize:{} developerId:{}", pageNumber, pageSize, developerId);
        BasePageResponse response = BasePageResponse.initialize();
        //get AccountCommon
        AccountCommon accountCommon = accountService.getAccountCommon(request);
        response = orgService.queryOrgByPage(accountCommon.getPlatform(), accountCommon.getRoleName(), accountCommon.getId(), pageNumber, pageSize, developerId);
        log.info("success exec method [queryOrgByPage]. result:{}", JsonUtils.objToString(response));
        return response;
    }

    /**
     * @param orgId
     * @return
     */
    @GetMapping(value = "/{orgId}/detail")
    public BaseResponse queryOrgDetail(@PathVariable("orgId") Integer orgId) {
        log.info("start exec method [queryOrgDetail]. orgId:{}", orgId);
        RspOrgVO orgVO = orgManager.queryOrgDetail(orgId);
        BaseResponse baseResponse = new BaseResponse(ConstantCode.SUCCESS, orgVO);
        log.info("success exec method [queryOrgDetail]. result:{}", JsonUtils.objToString(baseResponse));
        return baseResponse;
    }

    /**
     * @param orgId
     * @return
     */
    @DeleteMapping(value = "/{orgId}")
    public BaseResponse deleteOrg(@PathVariable("orgId") Integer orgId) {
        log.info("start exec method [deleteOrg]. orgId:{}", orgId);
        orgService.deleteByOrgId(orgId);
        log.info("success exec method [deleteOrg]");
        return new BaseResponse(ConstantCode.SUCCESS);
    }

}
